{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4d315122-a2e8-4df2-8c48-7df96a6f20aa",
   "metadata": {},
   "source": [
    "# Auxiliary Reuse and Management"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8647657-e9aa-4eea-a5d3-b146f56d5f10",
   "metadata": {},
   "source": [
    "This tutorial demonstrates automatic auxiliary qubits management by the synthesis engine. It synthesizes a simple state preparation function, which comprises several multi-controlled rotations. These rotations can use auxiliary qubits to reduce depth. For a given **global** width constraint, the initialization and reuse of auxiliary qubits between different function blocks is automated."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8a86fe71-0207-49f0-97db-ee484c0bc036",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:40:31.287243Z",
     "iopub.status.busy": "2024-05-07T13:40:31.286785Z",
     "iopub.status.idle": "2024-05-07T13:40:40.948807Z",
     "shell.execute_reply": "2024-05-07T13:40:40.948192Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/696c857c-20ab-4dad-acb9-3f7e98875450?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "from classiq import (\n",
    "    Constraints,\n",
    "    CustomHardwareSettings,\n",
    "    OptimizationParameter,\n",
    "    Preferences,\n",
    "    QArray,\n",
    "    QuantumProgram,\n",
    "    create_model,\n",
    "    prepare_amplitudes,\n",
    "    qfunc,\n",
    "    set_constraints,\n",
    "    set_preferences,\n",
    "    show,\n",
    "    synthesize,\n",
    ")\n",
    "\n",
    "NUM_QUBITS = 4\n",
    "np.random.seed(12)\n",
    "amplitudes = 1 - 2 * np.random.rand(2**NUM_QUBITS)\n",
    "amplitudes = (amplitudes / np.linalg.norm(amplitudes)).tolist()\n",
    "\n",
    "\n",
    "preferences = Preferences(\n",
    "    custom_hardware_settings=CustomHardwareSettings(basis_gates=[\"cx\", \"u\"]),\n",
    "    random_seed=1235,\n",
    "    optimization_timeout_seconds=100,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main() -> None:\n",
    "    out = QArray(\"out\")\n",
    "    prepare_amplitudes(amplitudes=amplitudes, bound=0.2, out=out)\n",
    "\n",
    "\n",
    "qmod = create_model(main)\n",
    "qmod = set_preferences(qmod, preferences)\n",
    "constraints = Constraints(\n",
    "    optimization_parameter=OptimizationParameter.DEPTH, max_width=8\n",
    ")\n",
    "qmod = set_constraints(qmod, constraints)\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "circuit = QuantumProgram.from_qprog(qprog)\n",
    "\n",
    "\n",
    "show(qprog)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
